package com.excel.read;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


public class TableDataExcelConfig extends ExcelConfig{
	ExcelTableManager pmbManager = new TableManagerImpl();

	public String tableConfig(String realPath) {
		File excel = null;
		
	
		Workbook w = null;
		try {
			excel = new File(realPath);
			w = Workbook.getWorkbook(excel);
			String names[] = w.getSheetNames();
			Sheet s = w.getSheet("数据");
			try {

				writeAllTable("", s);

			} catch (SameNameException e) {

			}
		} catch (Exception e) {

			System.out.println(e);
		} finally {
			w.close();
		}

		return "pmb";
	}

	public static int getRowNum(Sheet s, Cell aCell) {
		int row = s.getRows();
		int i = 0;
		for (i = aCell.getRow(); i < s.getRows(); i++) {
			Cell cell = s.getCell(1, i);
			String content = cell.getContents();
			if (content == null || content.trim().equals("")) {
				row = i - aCell.getRow();
				break;
			}
		}
		if (i == s.getRows()) {
			row = i - aCell.getRow();
		}
		return row;
	}
	
	public static List<Cell> findTables(Sheet s) {
		List<Cell> list = new ArrayList<Cell>();
		int curRowIndex = 1;
		int curColIndex = 0;
		int rows = s.getRows();
		int cols = s.getColumns();
		while (curRowIndex < s.getRows()) {
			Cell cell = s.getCell(1, curRowIndex);
			String content = cell.getContents();
			if (content != null && !content.trim().equals("")) {
				Cell aCell = s.getCell(1, curRowIndex - 1);
				content = aCell.getContents();
				if (content == null || content.trim().equals("")) {
					list.add(cell);
				}
			}
			curRowIndex++;
		}
		return list;
	}
	
public void writeAllTable(String filePath, Sheet s) throws BiffException,
	IOException, SameNameException {

AllTable allTable = Config.allTable;
List<Cell> list = findTables(s);
Cell dateCell = s.getCell(0, 0);
String aDate = "";
if(dateCell != null) {
	aDate = dateCell.getContents();
}


int maxPmbNo = (int) 1;

for (int m = 0; m < list.size(); m++) {
	Cell aCell = list.get(m);
	TableDataTable table = new TableDataTable();
	int rowNum = getRowNum(s, aCell);

	int colNum = getColNum(s, aCell);
	table.rowIndex = aCell.getRow();
	table.colIndex = aCell.getColumn() - 1;
	table.cols = colNum;
	table.rows = rowNum;
	aCell = s.getCell(table.colIndex, table.rowIndex - 1);

	table.name = aCell.getContents();
	
	table.pmbNo = maxPmbNo + m + 1;
	table.data = new String[table.rows][table.cols];
	int i = 0;
	int j = 0;
	for (i = 0; i < table.rows; i++) {
		for (j = 0; j < table.cols; j++) {
			Cell cell = s.getCell(j + table.colIndex, i
					+ table.rowIndex);
			String content = cell.getContents();
			table.data[i][j] = content;
		}
	}
	Cell cell = s.getCell(table.colIndex, i + table.rowIndex);
	String description = cell.getContents();
	table.setDescription(description);
	//allTable.setDataMap(table.name, table);

}

}
	

}
